using System;
using System.Web;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Users
{
    public partial class UserProfile : BaseControl
    {
        private static int userId;
        private readonly string MENUNAME = string.Empty;

        public UserProfile()
        {
        }

        public UserProfile(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                userId = ConvertUtility.ToInt32(Request.QueryString["uid"]);
                if (userId > 0)
                {
                    TimeZones.DataSource = StaticData.TimeZones;
                    TimeZones.DataBind();
                    lblCommandName.Text = MENUNAME;

                    PortalUser user = ManagerFactory.PortalUserManager.GetUser(userId);
                    if (user != null)
                    {
                        lblUsername.Text = user.USERNAME;
                        txtName.Text = user.NAME;
                        txtDisplayName.Text = user.DISPLAYNAME;
                        txtAddress.Text = user.ADDRESS;
                        txtEmail.Text = user.EMAIL;
                        txtTel.Text = user.TEL;
                        txtMobile.Text = user.MOBILE;
                        if (Session.GetUser().ISSUPERADMIN > 0)
                        {
                            pnlSuperadmin.Visible = true;
                            chkIsSuperAdmin.Checked = Convert.ToBoolean(user.ISSUPERADMIN);
                        }
                        else
                            pnlSuperadmin.Visible = false;
                        txtMobile.Text = user.MOBILE;
                        lblJoined.Text = user.JOINED.ToString("dd/MM/yyy HH:mm:ss");
                        lblLastVisited.Text = user.LASTVISIT.ToString("dd/MM/yyy HH:mm:ss");
                        TimeZones.SelectedValue = user.TIMEZONE.ToString();
                        PMNotification.Checked = ConvertUtility.ToBoolean(user.PMNOTIFICATION);
                    }
                }
                else
                    Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            userId = ConvertUtility.ToInt32(Request.QueryString["uid"]);
            if (userId > 0)
            {
                var user = new PortalUser();
                user.USERID = userId;
                user.NAME = txtName.Text;
                user.DISPLAYNAME = txtDisplayName.Text.Trim();
                user.EMAIL = txtEmail.Text;
                user.TEL = txtTel.Text.Trim();
                user.MOBILE = txtMobile.Text.Trim();
                user.ADDRESS = txtAddress.Text.Trim();
                user.IP = HttpContext.Current.Request.UserHostAddress;
                user.TIMEZONE = Convert.ToInt32(TimeZones.SelectedValue);
                user.PMNOTIFICATION = Convert.ToInt32(PMNotification.Checked);
                if (pnlSuperadmin.Visible)
                    user.ISSUPERADMIN = Convert.ToInt32(chkIsSuperAdmin.Checked);
                else
                    user.ISSUPERADMIN = 0;
                ManagerFactory.DAOEntity.Update(user);
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            else
                Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
        }

        protected void btnManagePassword_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30&t=3&uid=" + userId);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Default.aspx?p=3&l=vi-vn&m=30");
        }
    }
}